<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onsale">批量上架</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="offsale">批量下架</el-button>
        </el-form-item>



      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" height="500" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" fixed width="55">
      </el-table-column>
      <el-table-column prop="title" fixed label="产品名称" width="160">
      </el-table-column>
      <el-table-column label="封面" width="120">
        <template slot-scope="scope">
          <img :src=scope.row.cover style="width: 50px;height: 50px">
        </template>
      </el-table-column>
      <el-table-column prop="saleprice" label="原价" width="100">
      </el-table-column>
      <el-table-column prop="costprice" label="售价" width="100">
      </el-table-column>

      <el-table-column label="是否全新" min-width="50" width="100" >
        <template slot-scope="scope">
          <span v-if="scope.row.isnew===0" style="color: #1d8ce0">否</span>
          <span v-if="scope.row.isnew===1" style="color: #73e53e">是</span>
        </template>
      </el-table-column>

      <el-table-column prop="mileage" label="里程" min-width="100" >
      </el-table-column>

      <el-table-column prop="reigstertime" label="上牌时间" min-width="160" >
      </el-table-column>

      <el-table-column label="状态" min-width="50" width="80" >
        <template slot-scope="scope">
          <span v-if="scope.row.state===0" style="color: #e12d1b">下架</span>
          <span v-if="scope.row.state===1" style="color: #6ce788">上架</span>
        </template>
      </el-table-column>

      <el-table-column label="审核状态" min-width="50" width="100" >
        <template slot-scope="scope">
          <span v-if="scope.row.auditstate===0" style="color: #1bdae1">等待审核</span>
          <span v-if="scope.row.auditstate===1" style="color: #6ce788">审核通过</span>
          <span v-if="scope.row.auditstate===2" style="color: #e12d1b">审核驳回</span>
        </template>
      </el-table-column>

      <el-table-column prop="shopname" label="店铺名称" min-width="160" >
      </el-table-column>


      <el-table-column prop="carType.name" label="类型" min-width="50" >
      </el-table-column>

      <el-table-column prop="carType.name" label="急售" min-width="50" >
        <template slot-scope="scope">
          <span v-if="scope.row.rushsale===0" style="color: #1bdae1">否</span>
          <span v-if="scope.row.rushsale===1" style="color: #6ce788">是</span>
        </template>
      </el-table-column>

      <el-table-column prop="carType.name" label="是否超值" min-width="100" >
        <template slot-scope="scope">
          <span v-if="scope.row.costeffective===0" style="color: #1bdae1">否</span>
          <span v-if="scope.row.costeffective===1" style="color: #6ce788">是</span>
        </template>
      </el-table-column>

      <el-table-column  label="全国可迁" min-width="120" >
        <template slot-scope="scope">
          <span v-if="scope.row.transitivecountry===0" style="color: #1bdae1">否</span>
          <span v-if="scope.row.transitivecountry===1" style="color: #6ce788">是</span>
        </template>
      </el-table-column>

      <el-table-column prop="seller.username" label="销售顾问" min-width="120" >
      </el-table-column>

      <el-table-column label="操作" width="320" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="success" size="small" @click="handleResource(scope.$index, scope.row)">资源维护</el-button>
          <el-button size="small" @click="handleCheck(scope.$index, scope.row)">上传检测</el-button>
          <el-button size="small" @click="examine(scope.$index, scope.row)" v-if="scope.row.state===1||scope.row.state===4" type="warning" style="margin-top: 10px">审核</el-button>
          <el-button size="small" @click="activation(scope.$index, scope.row)" v-if="scope.row.state===2" type="success">激活</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :page-size="query.pageSize"
          :total="pageInfo.total"
          :current-page="query.currentPage"
          style="float:right;">
      </el-pagination>
    </el-col>



    <!--编辑/审核-->
    <el-dialog title="添加" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px"  ref="saveForm">
        <el-form-item label="商品名称" prop="username">
          <el-input v-model="saveForm.title"  auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="封面" >
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastdfs"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              multiple
              :limit="3"
              list-type="picture"
              :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="原价" prop="password">
          <el-input v-model="saveForm.saleprice"  auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="现价">
          <el-input v-model="saveForm.costprice"  ></el-input>
        </el-form-item>
        <el-form-item label="是否全新">
          <template>
            <el-radio v-model="saveForm.isnew" label="1">是</el-radio>
            <el-radio v-model="saveForm.isnew" label="0">否</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="上牌时间">
          <el-date-picker
              v-model="saveForm.reigstertime"
              type="date"
              placeholder="选择日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="里程数">
          <el-input v-model="saveForm.mileage" ></el-input>
        </el-form-item>

        <el-form-item label="是否超值">
          <template>
            <el-radio v-model="saveForm.costeffective" label="1">是</el-radio>
            <el-radio v-model="saveForm.costeffective" label="0">否</el-radio>
          </template>
        </el-form-item>

        <el-form-item label="全国可迁">
          <template>
            <el-radio v-model="saveForm.transitivecountry" label="1">是</el-radio>
            <el-radio v-model="saveForm.transitivecountry" label="0">否</el-radio>
          </template>
        </el-form-item>

        <el-form-item label="是否急售">
          <template>
            <el-radio v-model="saveForm.rushsale" label="1">是</el-radio>
            <el-radio v-model="saveForm.rushsale" label="0">否</el-radio>
          </template>
        </el-form-item>

        <el-form-item label="是否准新">
          <template>
            <el-radio v-model="saveForm.quasinewcar" label="1">是</el-radio>
            <el-radio v-model="saveForm.quasinewcar" label="0">否</el-radio>
          </template>
        </el-form-item>

        <el-form-item label="车辆类型">
          <el-cascader
              :options="carTypeTree"
              :props="{checkStrictly:true,label:'name',value:'id'}"
              v-model="saveForm.typeId">
          </el-cascader>
        </el-form-item>

        <el-form-item label="基本信息">
          <quill-editor v-model="saveForm.carDetail.info" :options="quilOption">
          </quill-editor>
        </el-form-item>



      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click.native="reject">取消</el-button>
        <el-button type="success" @click.native="saveSubmit">添加</el-button>
      </div>
    </el-dialog>

<!--    //资源维护-->
    <el-dialog title="资源维护" :visible.sync="RescourceFormVisible" :close-on-click-modal="false">
      <el-form :model="carResourcesForm" label-width="80px" :rules="saveFormRules" ref="carForm">
        <el-form-item label="主图">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastdfs/"
              :on-preview="mainimgHandlePreview"
              :on-remove="mainimgHandleRemove"
              :on-success="mainimgHandleSuccess"
              :file-list="mainimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="副图">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastdfs/"
              :on-preview="subimgHandlePreview"
              :on-remove="subimgHandleRemove"
              :on-success="subimgHandleSuccess"
              :file-list="subimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="更多图片">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastdfs/"
              :on-preview="moreimgHandlePreview"
              :on-remove="moreimgHandleRemove"
              :on-success="moreimgHandleSuccess"
              :file-list="moreimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="RescourceFormClose" >取消</el-button>
        <el-button type="primary" @click.native="saveCarResources" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

<!--    上传检测报告-->
<!--    检测报告-->
    <el-dialog title="检测报告" :visible.sync="TestReportFormVisible" :close-on-click-modal="false">
      <el-form :model="carResourcesForm" label-width="80px" :rules="saveFormRules" ref="carForm">
        <el-form-item label="主图">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastdfs/"
              :on-preview="mainimgHandlePreview"
              :on-remove="mainimgHandleRemove"
              :on-success="mainimgHandleSuccess"
              :file-list="mainimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="副图">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastdfs/"
              :on-preview="subimgHandlePreview"
              :on-remove="subimgHandleRemove"
              :on-success="subimgHandleSuccess"
              :file-list="subimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="更多图片">
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastdfs/"
              :on-preview="moreimgHandlePreview"
              :on-remove="moreimgHandleRemove"
              :on-success="moreimgHandleSuccess"
              :file-list="moreimgFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="TestReportFormClose" >取消</el-button>
        <el-button type="primary" @click.native="saveTestReportForm" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>


  </section>
</template>

<script>
// import util from '../../common/js/util'
// //import NProgress from 'nprogress'
// import { getcarListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';
import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import quilConfig from "../../common/js/quill-config"
export default {
  components:{
    quillEditor
  },
  data() {
    return {

      carResourcesForm:{
        car:{},
        mainImgList:[],
        subImgList:[],
        moreImgList:[]
      },
      coverFileList:[],
      mainimgFileList:[],
      subimgFileList:[],
      moreimgFileList:[],
      quilOption:quilConfig,
      carTypeTree:[],
      fileList:[],
      filters: {
        name: ''
      },
      total: 0,
      page: 1,
      listLoading: false,
      sels: [],//列表选中列
      TestReportFormVisible:false,//检测报告显示
      RescourceFormVisible:false,//显示
      saveFormVisible: false,//编辑/新增界面是否显示
      editLoading: false,
      saveFormRules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },

      //检测报告数据
      testForm:{
          id:null,
          infoText:'',
          carId:null,
      },
      //编辑界面数据
      saveForm: {
        title:"",
        cover:"",
        saleprice:null,
        costprice:null,
        isnew:1,
        reigstertime:null,
        mileage:null,//里程
        costeffective:1,//是否超值
        transitivecountry:null,
        rushsale:0,
        employee:{},
        typeId:null,
        quasinewcar:0,
        carType:{
          id:null,
          name:"",
        },
        carDetail:{
          info:''
        }

      },

      addLoading: false,
      addFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      query:{
        currentPage:1,  // 当前页
        pageSize:10, // 每页显示条数
        keyword:""  // 关键字-查询条件
      },
      pageInfo:{
        total:0,  // 总条数
        rows:[] // 当前页数据
      }

    }
  },
  methods: {


    onsale(){
      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if(!this.sels || this.sels.length  <1){
        this.$message({ message: '老铁，你不选中数据，臣妾上架不了啊....',type: 'error'});
        return;
      }
      this.$confirm('确认上架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/car/onsale',ids).then((res) => {
          this.listLoading = false;

          if(res.data.success){
            this.$message({
              message: res.data.message,
              type: 'success'
            });
            this.getcars()
          }else{
            this.$message({
              message: res.data.message,
              type: 'error'
            });

          }

        });
      }).catch(() => {

      });
    },
    offsale(){

      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if(!this.sels || this.sels.length  <1){
        this.$message({ message: '老铁，你不选中数据，臣妾下架不了啊....',type: 'error'});
        return;
      }
      this.$confirm('确认下架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/car/offsale',ids).then((res) => {
          this.listLoading = false;

          if(res.data.success){
            this.$message({
              message: '下架成功',
              type: 'success'
            });
            this.getcars()
          }else{
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }

        });
      }).catch(() => {

      });
    },

    //打开检测弹框
    handleCheck(){
      this.TestReportFormVisible=true
    },
    //关闭检测弹框
    TestReportFormClose(){
      this.TestReportFormVisible=false
    },

    saveCarResources(){
      console.log(this.carResourcesForm)
      if(this.carResourcesForm.subImgList.length>0 && this.carResourcesForm.mainImgList.length>0 && this.carResourcesForm.moreImgList.length>0)
      {

        this.$http.post("/carResources/save",this.carResourcesForm).then(res=>{
          if(res.data.success){
            this.RescourceFormVisible=false
          }

        })
      }else {
        this.$message.error("请至少选择一张图片")
      }

    },

    mainimgHandleSuccess(response, file, fileList){
     // console.log(response)
      this.carResourcesForm.mainImgList=[]
      fileList.forEach(item=>{
        console.log(item)
        this.carResourcesForm.mainImgList.push(item.url)
      })
      //console.log(this.carResourcesForm.mainImgList)

    },
    moreimgHandleSuccess(response, file, fileList){
      this.carResourcesForm.moreImgList=[]
      console.log(response)
      fileList.forEach(item=>{
        this.carResourcesForm.moreImgList.push(item.url)
      })

    },
    subimgHandleSuccess(response, file, fileList){
      this.carResourcesForm.subImgList=[]
      fileList.forEach(item=>{
        this.carResourcesForm.subImgList.push(item.url)
      })
    },

    mainimgHandleRemove(file, fileList){
      this.carResourcesForm.mainImgList=[]
      fileList.forEach(item=>{
        this.carResourcesForm.mainImgList.push(item.url)
      })
    },
    subimgHandleRemove(file, fileList){
      this.carResourcesForm.subImgList=[]
      fileList.forEach(item=>{
        this.carResourcesForm.subImgList.push(item.url)
      })
    },
    moreimgHandleRemove(file, fileList){
      this.carResourcesForm.moreImgList=[]
      fileList.forEach(item=>[
        this.carResourcesForm.moreImgList.push(item.url)
      ])
      //console.log(fileList)//
    },

    mainimgHandlePreview(){

    },
    subimgHandlePreview(){

    },
    moreimgHandlePreview(){

    },



    handleSuccess(res){
        this.saveForm.cover=res.data
    },
    handleRemove(){

    },
    handlePreview(){

    },

    //打开资源弹框
    handleResource(index,row){
      this.carResourcesForm.car=row
      this.RescourceFormVisible=true

      //console.log(this.carResourcesForm)

      //根据ID回显
      this.$http.get("/carResources/bycarId/"+row.id).then(res=>{

            if(res.data.success &&
              res.data.data.subImgList.length>0 && res.data.data.moreImgList.length>0
                && res.data.data.mainImgList.length>0
            )
            {
              this.subimgFileList=[]
              this.moreimgFileList=[]
              this.mainimgFileList=[]

              //将获取到的数据添加进回显列表
              res.data.data.subImgList.forEach(item=>{
                this.subimgFileList.push({name:name,url:item})
              })
              res.data.data.moreImgList.forEach(item=>{
                this.moreimgFileList.push({name:name,url:item})
              })
              res.data.data.mainImgList.forEach(item=>{
                this.mainimgFileList.push({name:name,url:item})
              })

            }

            //将回显列表的数据添加进 双向绑定的数据中
            this.mainimgFileList.forEach(item=>{
              this.carResourcesForm.mainImgList.push(item.url)
            })
            this.subimgFileList.forEach(item=>{
              this.carResourcesForm.subImgList.push(item.url)
            })
            this.moreimgFileList.forEach(item=>{
              this.carResourcesForm.moreImgList.push(item.url)
            })

            //console.log(this.carResourcesForm)

      }
      )

    },

    //关闭资源弹框
    RescourceFormClose(){
      this.RescourceFormVisible = false
      this.carResourcesForm={
        car:{},
        mainImgList:[],
            subImgList:[],
            moreImgList:[]
      }
      this.subimgFileList=[]
      this.moreimgFileList=[]
      this.mainimgFileList=[]
    },


    examine(index,row){
      this.saveFormVisible = true; // 展示弹框
      this.saveForm = Object.assign({}, row);// 将当前行的对象赋值给this.saveForm对象
      // 特殊处理manager parent
      // 如果manager parent没有值,为null的话,就给它们赋一个初识对象
      // if(!this.saveForm.admin){
      //   this.saveForm.admin = {
      //     id:null,
      //     username:""
      //   }
      // }
    },
    search(){
      this.query.currentPage = 1
      this.getcars()
    },
    //当页面被改变
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getcars();
    },
    //获取用户列表
    getcars() {
      this.$http.post("/car",this.query).then(result =>{
        result = result.data
        if(result.success){
          this.pageInfo = result.data
        }else {
          this.$message({message: result.message,type: 'error'});
        }
      }).catch(result =>{
        this.$message({message: "网络异常,请检查网络!",type: 'error'});
      })
    },
    //删除
    handleDel: function (index, row) {

      this.$confirm("当前状态为"+(row.state===1?'上架':'下架')+"是否删除",'提示',{}).then(() =>{
        // console.log("删除")
          let ids=[]
          ids.push(row.id)
          console.log(ids)
          this.$http({
            url:"/car/batch",
            method:"delete",
            data:ids
          }).then(res=>{
            if(res.data.success){
              this.$message.success("已删除")
              this.getcars()
            }
          })
      })
      // if(window.$confirm()){

      // }
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.saveFormVisible = true; // 展示弹框
      this.saveForm = Object.assign({}, row);// 将当前行的对象赋值给this.saveForm对象
      // 特殊处理manager parent
      // 如果manager parent没有值,为null的话,就给它们赋一个初识对象
      this.saveForm= {
        title:"",
        cover:"",
        saleprice:null,
        costprice:null,
        isnew:1,
        reigstertime:null,
        mileage:null,//里程
        costeffective:1,//是否超值
        transitivecountry:null,
        employee:{},
        typeId:null,
        quasinewcar:0,
        carType:{
          id:null,
          name:"",
        },
        carDetail: {
          info: ''
        }
      }
      // if(!this.saveForm.admin){
      //   this.saveForm.admin = {
      //     id:null,
      //     username:""
      //   }
      // }

    },
    //取消新增
    reject(){
      this.saveFormVisible=false
    },
    //显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      //获取树
      this.$http("/carType/tree").then(res=>{
        this.carTypeTree=res.data.data
      })
    },
    //编辑/新增的保存
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) =>{
        if(valid){
          this.$confirm('确定提交吗!','提示',{}).then(() =>{
            this.editLoading = true //加载框
            let params=this.saveForm
            let arry=params.typeId
            params.typeId=arry[arry.length-1]
            this.$http.put("/car",params).then(res=>{
              // console.log(res)
              if(res.data.success){
                this.saveFormVisible=false
              }
            })
            console.log(params)
          })
        }
      })
    },
    //新增
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      let ids = this.sels.map(x => x.id); // 取出它们的id,放到一个新数组中
      if(window.confirm("是否删除")){
        this.$http.patch("/car",ids).then(res =>{
          this.getcars()
          this.$message({
            message:"删除成功",
            type:"success"
          })
        })
      }
    }
  },
  mounted() {
    this.getcars();
  }
}

</script>

<style scoped>

</style>
